isImageUpdaterLoaded = true;
var inEditMode = false;
var updated = false;
var orgiandata = null;
var result = null;
var zoom_state = 0;
var updateurl = "index.php?mod=home&childmod=home&ajax=true&op=general_image_update";
var s_i_divid = "";
function edit() {
	if (!updated) {
		return;
	}
	inEditMode = true;
	$(".s_i_preview").cropper({
		zoomable : false,
		viewMode : 1
	});
	$(".s_cropper_editing").removeClass("disabled");
	$(".s_i_cut").addClass("disabled");
}

function reloadCropper() {
	if (inEditMode) {
		$(".s_i_preview").cropper("reset");
	}
}
function getImage() {
	return head_setting_image_data;
}

function dealImage() {
	fileObj = $("#s_i_input_updater")[0].files[0];
	if (fileObj == null || fileObj == undefined) {
		console.log("没有选择文件");
		return;
	}
	var reader = new FileReader();
	reader.readAsDataURL(fileObj);
	reader.onload = function(e) {
		$(".s_i_preview").attr("src", e.target.result);
		orgiandata = e.target.result;
	}
	$(".s_i_desc").html("你成功的选择了一张图片，如需裁剪和重新选择，请按下方对应按钮");
	updated = true;
	$(".s_cropper_update").addClass("disabled");
	$(".s_cropper_updated").removeClass("disabled");
}

function s_i_confirm() {
	if (updated) {
		if (inEditMode) {
			submitBlobFile(dataURLtoBlob($(".s_i_preview").cropper(
					"getCroppedCanvas").toDataURL('image/png')), updateurl,
					s_i_ajax_onError, s_i_ajax_onSuccess);
		} else {
			submitBlobFile(dataURLtoBlob(orgiandata), updateurl,
					s_i_ajax_onError, s_i_ajax_onSuccess);
		}
		inEditMode = false;
		updated = false;
		$(".s_i_preview").cropper("uncreate");
	} else {
		showCommonMessage("s_message", "warning", "你没有上传任何东西", 3000);
	}
}

function s_i_cancel() {
	if (updated) {
		updated = false;
	}
	if (inEditMode) {
		inEditMode = false;
		$(".s_i_preview").cropper("uncreate");
	}
	if (typeof (eval("image_cancel_callback")) == "function") {
		image_cancel_callback();
	}
}
function s_i_init() {
	if (inEditMode) {
		inEditMode = false;
		$(".s_i_preview").cropper("uncreate");
	}
	$(".s_i_preview").attr("src","");
	$(".s_i_confirm").addClass("disabled");
	$(".s_i_edit").addClass("disabled");
	$(".s_cropper_editing").addClass("disabled");
	$(".s_cropper_updated").addClass("disabled");
	$(".s_cropper_update").removeClass("disabled");
	$(".s_i_zoom_icon").removeClass("zoom-out").addClass("zoom-in");
}
function s_i_reset() {
	inEditMode = false;
	updated = false;
	orgiandata = null;
	result = null;
	s_i_divid = "";
	zoom_state = 0;
}
function s_i_reload() {
	if (inEditMode) {
		$(".s_i_preview").cropper("reset");
	}
	s_i_refresh();
}

function s_i_toogle_zoom() {
	if (zoom_state == 0) {
		zoom_state = 1;
		$(".s_i_zoom_icon").removeClass("zoom-in").addClass("zoom-out");
		$(".s_i_preview").removeClass("small");
	} else {
		zoom_state = 0;
		$(".s_i_zoom_icon").removeClass("zoom-out").addClass("zoom-in");
		$(".s_i_preview").addClass("small");
	}
}

function s_i_ajax_onError(data) {
	s_i_refresh();
	if (typeof (eval("image_update_callback")) == "function") {
		image_update_callback(false, data);
	}
}

function s_i_ajax_onSuccess(data) {
	s_i_refresh();
	if (typeof (eval("image_update_callback")) == "function") {
		image_update_callback(true, data);
	}
}

function s_i_setURL(newurl) {
	updateurl = newurl;
}

function s_i_refresh() {
	s_i_load(s_i_divid);
}
function s_i_load(divid) {
	$("#" + s_i_divid).html("");
	s_i_reset();
	s_i_divid = divid;
	$("#" + s_i_divid)
			.load(
					"index.php?mod=home&childmod=home&ajax=true&op=image_updater_insert");
}
function s_i_uninstall(){

	$("#" + s_i_divid).html("");
	s_i_reset();
}